package com.panshi.controller.usercenter;

import com.google.code.kaptcha.Producer;
import com.panshi.api.UserGetApi;
import com.panshi.utils.RedisUtils;
import com.panshi.vo.ResponseVO;
import com.panshi.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;


@Controller
public class ZhuceController {

    @Autowired
    Producer kaptchaProducer;//验证类

    @Autowired
    private UserGetApi userGetApi; //APi类

    @GetMapping("/zhuce") //注册页面
    public String zhuce() {

        return "zhuce";
    }
    //控制层调用验证码
    @GetMapping(path = "/kaptcha")
    @ResponseBody
    public void getKaptcha(HttpServletResponse response, HttpSession session) {
        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);
        System.out.println("kaptcha===="+session);
        System.out.println("sessionid =="+session.getId());
        //将验证码存入session中
        session.setAttribute("kaptcha", text);

        System.out.println("图形验证码：" + text);
        // 将突图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            System.out.println("响应验证码失败:" + e.getMessage());
        }
    }
    //注册按钮
    @PostMapping("/zhuceadd")
    @ResponseBody
    public ResponseVO addUser(Model model, HttpSession session,String userName, String pwd, String code){
        ResponseVO rs = userGetApi.addUser(userName,pwd,code);
        if(rs.isSuccess()){
            //注册成功
            System.out.println("注册成功==="+rs.getUserVO());
            session.setAttribute("loginUser",rs.getUserVO());
            model.addAttribute("loginUser",rs.getUserVO());
            rs.setUserVO(null);//不返回对象到前台
        }
        return rs;
    }
    //检测图形验证码是否正确
    @RequestMapping("/checkKaptcha")
    @ResponseBody
    public ResponseVO checkKaptcha(String userName,String imgcode,HttpSession session){
        ResponseVO rs=new ResponseVO();
        String kaptcha = session.getAttribute("kaptcha").toString();
        if(kaptcha.equals(imgcode)){
            //图形验证成功后还要判断用户是否存在 不存在才发送验证码
            UserVO userVO = userGetApi.findUserByEmail(userName);
            if(userVO != null){
                //该用户名存在 无法注册
                rs.setMsg("该账号已存在!");
                rs.setState(500);
                rs.setSuccess(false);
            }else{
                rs = userGetApi.sendEmail(userName,"zhuce"); //图形验证码验证成功就发送邮箱验证码
            }
        }else{
            rs.setMsg("图形验证码不匹配！请重新输入！");
            rs.setSuccess(false);
            rs.setState(500);
        }
        return rs;
    }
}
